//**************************************************************//
//  Effect File exported by RenderMonkey 1.6
//
//  - Although many improvements were made to RenderMonkey FX  
//    file export, there are still situations that may cause   
//    compilation problems once the file is exported, such as  
//    occasional naming conflicts for methods, since FX format 
//    does not support any notions of name spaces. You need to 
//    try to create workspaces in such a way as to minimize    
//    potential naming conflicts on export.                    
//    
//  - Note that to minimize resulting name collisions in the FX 
//    file, RenderMonkey will mangle names for passes, shaders  
//    and function names as necessary to reduce name conflicts. 
//**************************************************************//

//--------------------------------------------------------------//
// Render To Texture
//--------------------------------------------------------------//
//--------------------------------------------------------------//
// Pass 1
//--------------------------------------------------------------//
string Render_To_Texture_Pass_1_Teapot : ModelData = "..\\..\\..\\..\\Programme\\AMD\\RenderMonkey 1.82\\Examples\\Media\\Models\\Teapot.3ds";

texture renderTexture_Tex : RenderColorTarget
<
   float2 RenderTargetDimensions = {512,512};
   string Format="D3DFMT_A8R8G8B8";
   float  ClearDepth=1.000000;
   int    ClearColor=0;
>;
float4x4 matViewProjection;

float4x4 matProjection : Projection;

float3 lightPos
<
   string UIName = "lightPos";
   string UIWidget = "Numeric";
   bool UIVisible =  true;
   float UIMin = -1.00;
   float UIMax = 1.00;
> = float3( -21.10, -3.88, -207.45 );

float3 lightDir
<
   string UIName = "lightDir";
   string UIWidget = "Numeric";
   bool UIVisible =  true;
   float UIMin = -1.00;
   float UIMax = 1.00;
> = float3( 0.00, 0.00, 0.00 );



struct VS_INPUT 
{
   float4 Position : POSITION0;  
};


struct VS_OUTPUT
{
   float4 out_position:   POSITION0;
   float  out_tiefe:      TEXCOORD0;
};



VS_OUTPUT Render_To_Texture_Pass_1_Vertex_Shader_main( VS_INPUT Input )
{
   // Variablenvereinbarung
   VS_OUTPUT output;
   
   float3 z_Achse;
   float3 lightUp;  
   float3 x_Achse;  
   float3 y_Achse;
   float4 position;  

   
   z_Achse    =    normalize(lightDir - lightPos); 
   lightUp    =    normalize(cross(z_Achse, cross(float3(0.0, -1.0, 0.0), z_Achse))); 
   x_Achse    =    normalize(cross(lightUp,z_Achse)); 
   y_Achse    =    normalize(cross(z_Achse, x_Achse)); 

    
   position   =  Input.Position; 
   position  -=  float4(lightPos.x, lightPos.y, lightPos.z, 0.0); 
     
   position.x = dot(position, x_Achse); 
   position.y = dot(position,  y_Achse);   
   position.z = dot(position, z_Achse); 
    
   output.out_position = mul(position, matProjection); 
   output.out_tiefe = output.out_position.z; 
    
   return output;
}






float fFarClipPlane : FarClipPlane;
float fNearClipPlane : NearClipPlane;

struct PS_INPUT 
{
   float4 inp_position : POSITION0;
   float  inp_tiefe :    TEXCOORD0;
};

float4 Render_To_Texture_Pass_1_Pixel_Shader_main( PS_INPUT Input ) : COLOR0
{
   float erg; 
   
   erg = 1.0 - (Input.inp_tiefe / (fFarClipPlane - fNearClipPlane));
   
   return float4(erg , erg , erg , 1);
}


//--------------------------------------------------------------//
// Pass 2
//--------------------------------------------------------------//
string Render_To_Texture_Pass_2_Teapot : ModelData = "..\\..\\..\\..\\Programme\\AMD\\RenderMonkey 1.82\\Examples\\Media\\Models\\Teapot.3ds";

float4x4 Render_To_Texture_Pass_2_Vertex_Shader_matProjection : Projection;
float4x4 matTeapotXForm
<
   string UIName = "matTeapotXForm";
   string UIWidget = "Numeric";
   bool UIVisible =  false;
> = float4x4( 1.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 1.00, 210.08, 0.00, 0.00, 0.00, 1.00 );

float3 Render_To_Texture_Pass_2_Vertex_Shader_lightPos;
float3 Render_To_Texture_Pass_2_Vertex_Shader_lightDir;

struct Render_To_Texture_Pass_2_Vertex_Shader_VS_OUTPUT
{
   float4 Pos: POSITION;
   float2 Tex: TEXCOORD0;
};
Render_To_Texture_Pass_2_Vertex_Shader_VS_OUTPUT Render_To_Texture_Pass_2_Vertex_Shader_main( float4 Pos: POSITION,
                float2 Tex:  TEXCOORD0 )
{
   Render_To_Texture_Pass_2_Vertex_Shader_VS_OUTPUT Out = (Render_To_Texture_Pass_2_Vertex_Shader_VS_OUTPUT) 0; 
   
   Out.Pos = mul( Pos, mul(matTeapotXForm,Render_To_Texture_Pass_2_Vertex_Shader_matProjection));
   
   Out.Tex = Tex;
   return Out;
}








float4 ambient
<
   string UIName = "ambient";
   string UIWidget = "Color";
   bool UIVisible =  true;
> = float4( 0.50, 0.50, 0.50, 1.00 );
sampler renderTexture = sampler_state
{
   Texture = (renderTexture_Tex);
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
   MIPFILTER = LINEAR;
};
float4 Render_To_Texture_Pass_2_Pixel_Shader_main( float4 Diff: COLOR0,
             float2 Tex: TEXCOORD0 ) : COLOR
{
    return tex2D( renderTexture, Tex ) + ambient;
}





//--------------------------------------------------------------//
// Technique Section for Render To Texture
//--------------------------------------------------------------//
technique Render_To_Texture
{
   pass Pass_1
   <
      string Script = "RenderColorTarget0 = renderTexture_Tex;"
                      "ClearColor = (0, 0, 0, 0);"
                      "ClearDepth = 1.000000;";
   >
   {
      VertexShader = compile vs_1_1 Render_To_Texture_Pass_1_Vertex_Shader_main();
      PixelShader = compile ps_2_0 Render_To_Texture_Pass_1_Pixel_Shader_main();
   }

   pass Pass_2
   {
      VertexShader = compile vs_1_1 Render_To_Texture_Pass_2_Vertex_Shader_main();
      PixelShader = compile ps_2_0 Render_To_Texture_Pass_2_Pixel_Shader_main();
   }

}

